package a4;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {

    public void setName() {

    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        int count = 10000000;
        long start = System.currentTimeMillis();
        ReflectTest test = new ReflectTest();

        for (int i = 0; i < count; i++) {
            Method setName = ReflectTest.class.getDeclaredMethod("setName");
            setName.invoke(test);
        }
        System.out.println("反射耗时：" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        Method setName = ReflectTest.class.getDeclaredMethod("setName");
        for (int i = 0; i < count; i++) {
            setName.invoke(test);
        }
        System.out.println("反射耗时2：" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            test.setName();
        }
        System.out.println("反射耗时3：" + (System.currentTimeMillis() - start));

    }
}
